<template>
  <MainSlot v-loading="loading">
    <template v-slot:searchForm>
      <SearchForm :formItemList="formItemList" @search="search"></SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <TableData :config="table_config" :isSelect="true" :tableThead="tableThead" :dataList="dataList"
        :loading="loading" @selection-change="selectionChange">
        <template v-slot:enableStatus="{ row }">
          <span>{{ row.enableStatus ? '已启用' : '已停用' }}</span>
        </template>
      </TableData>
    </template>
    <template v-slot:dialogModule>
      <AddEdit :title="'仓库'" @closeDialog="closeDialog" :addEditFlag="addEditFlag" :dialogVisible="dialogVisibleAddEdit"
        :data="formData" />
    </template>
    <template v-slot:pagination>
      <Pagination @size-Change="handleSizeChange" :size="size" @current-change="handleCurrentChange" :page="page"
        :total="total">
      </Pagination>
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components/MainSlot";
import SearchForm from '@/components/SearchForm';
import TableData from '@/components/TableData';
import Pagination from '@/components/Pagination';
import warehouseManage from '@/api/digtalOperation/warehouseManage';
import BtnSlot from '@/components/BtnSlot';
import AddEdit from './components/addEdit';

export default {
  components: { MainSlot, SearchForm, TableData, Pagination, BtnSlot, AddEdit },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      dialogVisibleAddEdit: false,
      dialogVisibleAddDetailsInfo: false,
      addEditFlag: 'add',
      btnList: ['add', 'delete'],
      formData: {},
      detailsInfo: {},
      searchFormData: {},
      page: 1,
      size: 10,
      total: 10,
      dataList: [],
      invcountDetailList: [],
      selectDataList: [],
      materialList: [],
      tableThead: [
        { isShow: true, label: '仓库名称', prop: 'stordocName', minWidth: '120px' },
        { isShow: true, label: '仓库位置', prop: 'address' },
        { isShow: true, label: '仓库类型', prop: 'stordocType' },
        { isShow: true, label: '负责人', prop: 'personName', minWidth: '120px' },
        { isShow: true, label: '电话号码', prop: 'phone' },
        { isShow: true, label: '启用状态', slotName: 'enableStatus', },
        { isShow: true, label: '备注', prop: 'remark', },

      ],
      // 配置项
      table_config: {
        isCheckBox: true,

        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'delete',
              handleRow: this.handleRow
            }
          ]
        },
      },
      formItemList: [
        {
          label: '仓库类型',
          type: 'select',
          selectOptions: [
            { text: "原料", value: "原料" },
            { text: "半成品", value: "半成品" },
            { text: "成品", value: "成品" }
          ],
          param: 'stordocType',
          defaultSelect: '',
          multiple: false,
          isShow: true
        },
        {
          label: '仓库名称',
          type: 'input',
          param: 'stordocName',
          isShow: true
        },
        // {
        //   label: '仓库管员',
        //   type: 'select',
        //   selectOptions: this.setOptions('cwhsmanagerName'),
        //   param: 'cwhsmanagerName',
        //   defaultSelect: '',
        //   multiple: false,
        //   isShow: true
        // },
        // {
        //   label: '盘点人',
        //   type: 'select',
        //   selectOptions: this.setOptions('cwhsmanagerName'),
        //   param: 'countoperatorName',
        //   defaultSelect: '',
        //   multiple: false,
        //   isShow: true
        // }
      ]
    }
  },
  computed: {
    person() {
      return this.$store.state.initData.person || [];
    },

  },
  mounted() {
    this.queryPageByCondition()
  },
  methods: {
    setOptions(param) {
      let options = [];
      if (param == 'stordocNo') {
        this.$store.state.initData.stordoc.forEach(item => {
          options.push(
            { text: item.stordocName, value: item.stordocNo }
          )
        })
      } else if (param == 'cwhsmanagerName') {
        this.$store.state.initData.person.forEach(item => {
          options.push(
            { text: item.person.name, value: item.person.name }
          )
        })
      }
      return options;
    },
    // 操作方法
    handleRow(rowData, item) {
      console.log(rowData, item);
      if (item.flag == 'delete') {
        this.delList([rowData])
      } else if (item.flag == 'edit') {
        this.dialogVisibleAddEdit = true;
        this.formData = JSON.parse(JSON.stringify(rowData))
      }
      this.addEditFlag = item.flag
    },
    //获取选中Btn
    getBtnEvent(flag) {
      console.log(flag)
      if (flag == 'add') {
        this.dialogVisibleAddEdit = true
      } else if (flag == 'delete') {
        if (this.selectDataList.length > 0) {
          this.delList(this.selectDataList)
        } else {
          this.$message.warning('请先选择表单数据项后再删除！')
        }
      }
      this.addEditFlag = flag
    },
    lookView(row) {
      let params = {
        invcountNo: row.invcountNo
      }
      warehouseManage.invcountDetail(params).then(res => {
        console.log(res)
        let { data } = res
        this.invcountDetailList = data || [];
        this.dialogVisible = true;
      }).catch(err => {
        console.log(err)
      })
    },
    addDetailsInfo(row) {
      this.detailsInfo = JSON.parse(JSON.stringify(row))
      warehouseManage.queryMaterialByStordocNo(row.stordocNo).then(res => {
        console.log(res)
        if (res.code = 1) {
          this.materialList = res.data || [];
          this.dialogVisibleAddDetailsInfo = true;
        } else {
          this.$message.warning('查询仓库物料失败!')
        }
      }).catch(err => {
        console.log(err)
      })
    },
    search(data) {
      console.log(data)
      this.page = 1;
      this.searchFormData = { ...data }
      this.queryPageByCondition()
    },
    handleCurrentChange(page) {
      this.page = page;
      this.queryPageByCondition();
    },
    handleSizeChange(size) {
      this.size = size;
      this.queryPageByCondition();
    },
    queryPageByCondition() {
      let params = {
        page: this.page,
        size: this.size,
        entity: { ...this.searchFormData }
      }
      warehouseManage.pageQuery(params).then(res => {
        let { data } = res
        this.dataList = data.content || [];
        this.total = data.totalElements;
        if (res.data.totalPages && this.page > res.data.totalPages) {
          this.page = res.data.totalPages;
          this.queryPageByCondition();
        }
      }).catch(err => {

      })
    },
    delList(list, key = 'stordocNo') {
      let noList = [];
      list.forEach(item => {
        noList.push(item[key])
      })
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        warehouseManage.delStordocList(noList).then(res => {
          if (res.code == 1) {
            this.$message.success('删除成功')
            this.queryPageByCondition();
          } else {
            this.$message.warning('删除失败')
          }
        }).catch(err => {
          this.$message.warning('删除失败')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    selectionChange(data) {
      this.selectDataList = data;
    },
    //关闭弹窗
    closeDialog() {
      this.dialogVisibleAddEdit = false;
      this.dialogVisibleAddDetailsInfo = false;
      this.queryPageByCondition();
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => { });
    }
  }
};
</script>
<style lang="scss" scoped>
.info {
  box-shadow: 0 0 10px 10px #dddddd5d;
  padding: 15px;
  border-radius: 8px;
  transition: all 0.2s;

  &:hover {
    box-shadow: 0 0 10px 10px #dddddd9f;
  }

  .label {
    padding-bottom: 10px;
    margin-bottom: 10px;
    font-size: 14px;
    font-weight: bold;
    color: #333333;
    border-bottom: 1px solid #f0f0f0;
  }

  .invcountDetailInfo {
    display: flex;
    flex-wrap: wrap;
    width: 100%;

    .item {
      width: calc(100% / 3);
      padding-bottom: 10px;
      color: #333333;

      .name {
        color: #333333;
        font-weight: bold;
      }
    }
  }

}
</style>